<?php
namespace Crm\Controller;

use Zend\View\Model\ViewModel;
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Worksheet;
use Crm\Common\CommonHelper;
use Crm\Filter\CommonFilter;

class FinancialController extends AbstractController
{

    public function indexAction()
    {
        $cityList = array();
        $cityList = $this->getCityTable()->find();
        $catalogList = array();
        $catalogList = $this->getCatalogTable()->find();
        $organizationList = array();
        $organizationList = $this->getOrganizationTable()->findAll();
        return new ViewModel(array(
            'city'=>$cityList,
            'catalog'=>$catalogList,
            'organization'=>$organizationList,
        ));
    }
    
    
    public function ajaxOrderListAction()
    {
    
        $query = $this->request->getPost()->toArray();
    
        $paginator = $this->getOrderTable()->getOrderJiesuList($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }
    public function excelBalanceAction()
    {
        $query = $this->request->getQuery()->toArray();
        $outExcelList = array();
        $outExcelList = $this->getOrderTable()->getExcelJiesuList($query);
        
        $objPHPExcel = new PHPExcel();
        //sheet1 订单列表
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '订单NO')
        ->setCellValue('B1', '城市')
        ->setCellValue('C1', '商户类型')
        ->setCellValue('D1', '商户ID')
        ->setCellValue('E1', '商户名称')
        ->setCellValue('F1', '用户ID')
        ->setCellValue('G1', '订单总价')
        ->setCellValue('H1', '订单时间');

        //详细数据
        foreach ($outExcelList as $key=>$order){
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.($key + 2), $order['order_no'])
            ->setCellValue('B'.($key + 2), $order['city_name'])
            ->setCellValue('C'.($key + 2), $order['catalog_name'])
            ->setCellValue('D'.($key + 2), $order['org_id'])
            ->setCellValue('E'.($key + 2), $order['org_name'])
            ->setCellValue('F'.($key + 2), $order['user_id'])
            ->setCellValue('G'.($key + 2), $order['order_total_price'])
            ->setCellValue('H'.($key + 2), $order['order_create_time']);
        }
        
        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('订单列表');
        
        //sheet2 订单商品明细
        $pSheet = new PHPExcel_Worksheet($objPHPExcel,'商品销量统计');
        $outProDetailList = array();
        $outProDetailList = $this->getOrderTable()->getExcelJiesuPro($query);
        $pSheet
        ->setCellValue('A1', '商品ID')
        ->setCellValue('B1', '商品名称')
        ->setCellValue('C1', '销售数量');
        //详细数据
        foreach ($outProDetailList as $key=>$order){
            $pSheet
            ->setCellValue('A'.($key + 2), $order['product_id'])
            ->setCellValue('B'.($key + 2), $order['oi_name'])
            ->setCellValue('C'.($key + 2), $order['count']);
        }
        // Rename worksheet
        $objPHPExcel->addSheet($pSheet);
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 生成excel文件 Excel2007
        CommonHelper::browserExport('Excel5', '结算_'.date('YmdHis').'.xls'); // 输出到浏览器
        $objWriter->save("php://output");
        exit();
    }
    public function exportAction()
    {
        $request = $this->request->getQuery()->toArray();
        
        (int) $user_uid = isset($request['user_uid']) ? $request['user_uid'] : 0;
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        $start_time = isset($request['start_time']) ? $request['start_time'] : '';
        $end_time = isset($request['end_time']) ? $request['end_time'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
      
        $query = array(
            'user_uid' => $user_uid,
            'user_name' => $user_name,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'page' => $page
        );
        
        $paginator = $this->getUserBalanceLogTable()->getOutExcelList($query);
        
        $objPHPExcel = new PHPExcel();
        // $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        // ->setLastModifiedBy("Maarten Balliauw")
        // ->setTitle("Office 2007 XLSX Test Document")
        // ->setSubject("Office 2007 XLSX Test Document")
        // ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        // ->setKeywords("office 2007 openxml php")
        // ->setCategory("Test result file");
        
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()
            ->setCellValue('A1', "用户名")
            ->setCellValue('B1', "用户编号")
            ->setCellValue('C1', "用户余额")
            ->setCellValue('D1', "收入金额")
            ->setCellValue('E1', "支出金额")
            ->setCellValue('F1', "账户余额")
            ->setCellValue('G1', "交易时间")
            ->setCellValue('H1', "备注")
            ->setCellValue('I1', "详情");
        
        $n = 2;
        foreach ($paginator as $k => $v) {
            
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $n, $v['user_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $n, $v['user_uid']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $n, $v['balance']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $n, $v['ubl_income']);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $n, $v['ubl_outcome']);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $n, $v['ubl_balance']);
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $n, $v['ubl_create_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $n, $v['ubl_remark']);
            $objPHPExcel->getActiveSheet()->setCellValue('I' . $n, $v['ubl_detail']);
            $n += 1;
        }
        
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 生成excel文件 Excel2007
        // $objWriter->save($dir."/export_1.xls");//保存文件
        CommonHelper::browserExport('Excel5', 'Financial' . date("Ymd") . '.xls'); // 输出到浏览器
        $objWriter->save("php://output");
        exit();
    }

    public function listAction()
    {
        return new ViewModel();
    }

    public function ajaxFinancialListAction()
    {

        $request = $this->request->getPost()->toArray();
        
        (int) $user_uid = isset($request['user_uid']) ? $request['user_uid'] : 0;
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        $start_time = isset($request['start_time']) ? $request['start_time'] : '';
        $end_time = isset($request['end_time']) ? $request['end_time'] : '';
        
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'user_uid' => $user_uid,
            'user_name' => $user_name,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'page' => $page
        );
        
        $paginator = $this->getUserBalanceLogTable()->find($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }
}
